import {CommonSvc} from './commonsvc.js';
import {LGXEnums}  from '../common/sysenum.js';
import {CoordTRFUtil}  from '../common/tools/coordtool.js';
import { CGraphTypeUtil } from '../graph/paintutil/graphtypeutil.js';

import LGXCOM from '../common/common.js'
import utilfuncs from '../common/tools/utilfuncs.js';
var funcs = utilfuncs.UtilFuncs;

import CopyMemBufManager from './copypaste/copymembufanager.js';
import {KeyGenerator}  from '../common/keygenerator.js';

class CopyPasteSvc {
    constructor(manager, mainControler, rectSelService) {
        this.dataManger = manager;
        this.parentControler = mainControler;
        this.m_pRectSelectService = rectSelService;
        this.m_pBufManage = null;

        this.m_nMultiSel = 0;
        this.m_nHaveCopyInfo = 0;
        this.m_tLastPastGraphs = new Array();
    }

    copy() {
        let nFailed = 0;
        this.m_tLastPastGraphs.length = 0;
        var tSelect = new Array();
        this.getCopySelGraphs(tSelect);
        this.m_nHaveCopyInfo = tSelect.length;
        if (this.m_nHaveCopyInfo == 0) {
            return 0;
        }

        var rect = CommonSvc.calGraphsZone(tSelect);
        if (rect.width() > 0) {
            nFailed = this.copyInfo(tSelect, rect.getCnt());
            this.m_tCopyCenterPt = rect.getCnt();
            this.dataManger.setMultiModifyMode(false);
            this.parentControler.setMapWorkMode(LGXEnums.LGXWorkMode.ModifyMode);
        }
        else {
            nFailed--;
        }
        return nFailed;
    }

    getCopySelGraphs(tDestSelectLst) {
        var pMulSelRect = this.dataManger.getMulSelRectGraph();
        if (pMulSelRect != null) {
            var tSelGraphLst = new Array();
            let layers = this.dataManger.getLayerManager();
            layers.chkInSelRect(pMulSelRect.getZone(), tSelGraphLst);
            tSelGraphLst.forEach((p) => {
                this.addGraphLst(p, tDestSelectLst);
            });
        }

        let pGraph = this.dataManger.getCurentGraph();
        if (pGraph) {
            this.addGraphLst(pGraph, tDestSelectLst);
        }

        for (let i = 0; i < tDestSelectLst.length; i++) {
            let pGraph = tDestSelectLst[i];
            let pSupContainer = pGraph.getContainer();
            if (pSupContainer) {
                let list = pSupContainer.getChildList();
                list.forEach((p) => {
                    this.addGraphLst(p, tDestSelectLst);
                });
                this.addGraphLst(pSupContainer, tDestSelectLst);
            }
        }

        let count = tDestSelectLst.length;
        for (let i = count - 1;i >= 0;i--){
            let graph = tDestSelectLst[i];
            if (!CGraphTypeUtil.isRealGraph(graph.m_graphType)){
                tDestSelectLst.splice(i,1);
            }
        }
        

        this.dataManger.clearSelectList();
        this.dataManger.clearBorderList();
        return 0;
    }

    addGraphLst(pGraph, tSelectLst) {
        let nExist = tSelectLst.indexOf(pGraph);
        if (nExist == -1) {
            tSelectLst.push(pGraph);
        }
    }

    copyInfo(tSelectLst, pt) {
        let nCount = tSelectLst.length;
        var tGraph = new Array();
        var tRelateResLst = new Array();

        for (let i = 0; i < nCount; i++) {
            let pGraph = tSelectLst[i];
            tGraph.push(pGraph);
            let label = pGraph.getLabel();
            if (label && tSelectLst.indexOf(label) == -1){
                tGraph.push(label);
            } 
        }

        if (this.m_pBufManage != null) {
            this.m_pBufManage.clear();
            this.m_pBufManage = null;
        }
        this.m_pBufManage = new CopyMemBufManager.CopyMemBufManager();
        this.m_pBufManage.CopyFromGraphLst(tGraph, pt.x, pt.y);
        this.m_pBufManage.CopyFromResLst(tRelateResLst, tGraph, pt.x, pt.y);
        return 0;
    }

    paste(tPoint) {
        let pMapPos = this.dataManger.getMapViewInfo();
        let nFailed = 0;
        let tWorldPoint = new LGXCOM.LGXPoint();
        tWorldPoint = CoordTRFUtil.screen2world(tPoint, pMapPos);
        var tGraphList = new Array();
        this.dataManger.m_pOperStepService.record(1, tGraphList);
        nFailed = this.PasteInfo(tWorldPoint);
        this.dataManger.m_pOperStepService.record(0, tGraphList);
        this.dataManger.setMultiModifyMode(false);
        this.parentControler.setMapWorkMode(LGXEnums.LGXWorkMode.ModifyMode);
        //SetCurntPasteGraphsSeled();
        return nFailed;
    }


    PasteInfo(pasteOrgPt) {
        if (this.m_pBufManage == null) {
            return 1;
        }
        var tGraph = new Array();
        var tRes = new Array();

        this.PasteGraphInfo(tGraph);
        this.PasteNodeInfo(tRes);
        this.PasteLineSegInfo(tRes);
        this.pasteMoldTopoInfo(tRes);
        this.m_pBufManage.copySrcPropToDest(tGraph, tRes, pasteOrgPt.x, pasteOrgPt.y);

        let mapscale = this.dataManger.getMapScale();
        this.m_tLastPastGraphs.length = 0;
        var that = this;

        tGraph.forEach((pGraph) => {
            var devSymbol = that.dataManger.getSymbol(pGraph.getType(), 0);
            if (devSymbol) {
                pGraph.setSymbol(devSymbol);
            }
            pGraph.setUseful(1);
            pGraph.setStart(1);
            pGraph.setFinish(1);
            pGraph.calScope(mapscale);
            that.dataManger.recordRepaintScope(pGraph);
            that.m_tLastPastGraphs.push(pGraph);
        });

        tRes.forEach((pRes) => {
            pRes.setUseful(1);
        });

        this.m_pBufManage.makeTopo(tGraph);

        return 0;
    }

    productGraph4Paste(type) {
        let newGraph = this.dataManger.productGraph(type);
        if (newGraph){
            newGraph.m_gid = KeyGenerator.gid();
        }
        else{
            console.log("error*****");
        }
        return newGraph;
    }

    PasteGraphInfo(tGraphList) {
        let nGraph = this.m_pBufManage.m_tGraphBuf.length;
        for (let i = 0; i < nGraph; i++) {
            let pData = this.m_pBufManage.m_tGraphBuf[i];
            let pNewGraph = this.productGraph4Paste(pData.type);
            if (pNewGraph){
                pNewGraph.setUseful(0);
                tGraphList.push(pNewGraph);
                this.m_pBufManage.AddIDMap(pData.gid, pNewGraph.m_gid);
            }
            else{
                console.log('past error graph');
            }
        }
    }

    PasteNodeInfo(list) {
        let pResManager = this.dataManger.getTopoManager();
        let nNode = this.m_pBufManage.m_tNodeBuf.length;
        for (let i = 0; i < nNode; i++) {
            let pData = this.m_pBufManage.m_tNodeBuf[i];
            let pRes = pResManager.newPowerResource(pData.type, 0, pData.m_nNewID);
            if (pRes) {
                pRes.setUseful(0);
                list.push(pRes);
                this.m_pBufManage.AddIDMap(pData.id, pRes.id);
            }
        }
        return 0;
    }

    PasteLineSegInfo(list) {
        let pResManager = this.dataManger.getTopoManager();
        let nVirSeg = this.m_pBufManage.m_tLinehBuf.length;
        for (let i = 0; i < nVirSeg; i++) {
            let pData = this.m_pBufManage.m_tLinehBuf[i];
            let pRes = pResManager.newPowerResource(global.ConnectLine, pData.m_nNewGraphicID, pData.m_nNewID);
            if (pRes) {
                pRes.setUseful(0);
                list.push(pRes);
                this.m_pBufManage.AddIDMap(pData.id, pRes.id);
            }
        }
        return 0;
    }

    pasteMoldTopoInfo(list) {
        let pResManager = this.dataManger.getTopoManager();
        let devCount = this.m_pBufManage.m_tDevResBuf.length;
        for (let i = 0; i < devCount; i++) {
            let pData = this.m_pBufManage.m_tDevResBuf[i];
            let nType = funcs.mainObjType(pData.id);
            let pRes = pResManager.newPowerResource(nType, pData.m_nNewGraphicID, pData.m_nNewID);
            if (pRes) {
                pRes.setUseful(0);
                list.push(pRes);
                this.m_pBufManage.AddIDMap(pData.id, pRes.id);
            }
        }
    }

}

export default { CopyPasteSvc }